package com.alb.core.netty;

import com.alb.core.context.HttpRequestWrapper;
import com.alb.core.netty.processor.NettyProcessor;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpRequest;

/**
 * 自制nettyHandler
 * 这个handler实际还是依靠nettyProcessor去处理
 * @author MattS
 * @date 2024/04/25
 */
public class NettyHttpServerHandler extends ChannelInboundHandlerAdapter {
    private final NettyProcessor nettyProcessor;
    public NettyHttpServerHandler(NettyProcessor processor) {
        this.nettyProcessor = processor;
    }
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //先将消息强制转换为FullHttpRequest
        FullHttpRequest request = (FullHttpRequest) msg;
        HttpRequestWrapper wrapper = new HttpRequestWrapper();
        wrapper.setRequest(request);
        wrapper.setCtx(ctx);
        nettyProcessor.process(wrapper);
    }
}
